/**
 * 用户反馈
 * @author truexin
 * @date   2016/06/12
 */

(function (lf, r) {

    var RC = r.createClass,
        ajax = lf.utils.ajax,
        content = '';

    var Body = RC({
        getInitialState: function () {
            return ({
                len: ''
            })
        },
        checkLen: function () {
            var len = $('#text').val().length;
            this.setState({
                len: len
            })
        },
        checkSubmit: function () {
            content = $(r.findDOMNode(this.refs.text)).val();
            if (!content) return false;
            return content;
        },
        submit: function () {
            var me = this,
                houseId = this.props.user.houseId,
                residentId = this.props.user.id,
                type = this.props.user.householdType;
            type = type == '1' ? '1' : '0';
            if (me.checkSubmit()) {
                ajax({
                    type: 'post',
                    contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
                    data: {
                        houseId: houseId,
                        residentId: residentId,//这里可能出错，用的是id
                        isProprietor: type,//1为业主，0为住户
                        content: content,
                    },
                    success: function (data) {
                        if (data.code == '200') {
                            mui.toast('谢谢您的反馈O(∩_∩)O');
                            location.reload();
                        }
                    }
                }, lf.finger.api.user.createFeedback);
            } else {
                mui.toast('请先填写反馈内容');
            }
            return;
        },

        render: function () {

            return (
                <div className="feedback-page">
                    <textarea id='text' onChange={this.checkLen} name="content" ref="text" cols="30" rows="10"
                              placeholder="请输入反馈内容..."></textarea>
                    <p>您已经输入{this.state.len || '0'}个字</p>
                    <button ref='button' className="mui-btn mui-btn-block save-btn" type="submit" onClick={this.submit}>
                        发 表
                    </button>
                </div>
            )
        }
    });

    lf.bridgeJs.getUser(function (user) {
        r.render(
            <Body user={user}/>,
            document.getElementById('root'), function () {
                $('#root').css('background', '#f4f4f4');
            }
        )
    })

})(liefeng, React)

